import QtQuick 1.1
import com.nokia.meego 1.0
import RpcConnection 1.0
import DeviceList 1.0
import ActionMapper 1.0
import InputContextRpc 1.0

PageStackWindow {
    id: appWindow

    initialPage: targetsPage
    property alias rpc : rpcClient
    property alias devices : deviceList

    DeviceList {
        id: deviceList
    }

    // this Timer and handleConnected make sure that the pageStack animation is finished before setting a new page
    Timer {
        id: connectionSettleTimer
        onTriggered: appWindow.handleConnected()
    }

    function handleConnected() {
        if (appWindow.pageStack.busy)
            connectionSettleTimer.restart()
        else
            appWindow.pageStack.push(controlPage)
    }

    InputContextRpc {
        id: inputContextRpc

        onInputPanelStartRequested: {
            controlPage.showTextInput()
        }
        onInputPanelStopRequested: {
            controlPage.showButtonInput()
        }
    }

    RpcConnection {
        id: rpcClient
        property string source
        property int position

        onClientConnected: {
            appWindow.handleConnected()
        }

        onClientDisconnected: {
            appWindow.pageStack.pop(targetsPage)
        }

        function send(ip, port, src, pos) {
            source = src
            position = pos
            connectToHost(ip, port);
        }
    }

    TargetsPage {
        id: targetsPage
        onConnectToIp: {
            appWindow.pageStack.push(busyPage)
            rpcClient.connectToHost(ip, 1234)
        }
    }

    ControlPage {
        id: controlPage
    }

    BusyPage {
        id: busyPage
    }

    ToolBarLayout {
        id: commonTools
        visible: true
        ToolIcon {
            platformIconId: "toolbar-view-menu";
            anchors.right: parent===undefined ? undefined : parent.right
            onClicked: (myMenu.status == DialogStatus.Closed) ? myMenu.open() : myMenu.close()
        }
        ToolIcon {
            platformIconId: "toolbar-back";
            anchors.left: parent===undefined ? undefined : parent.left
            onClicked: { appWindow.rpc.disconnectFromHost(); appWindow.pageStack.pop(targetsPage) }
            opacity: appWindow.pageStack.depth > 1 ? 1 : 0
            Behavior on opacity {
                NumberAnimation {}
            }
        }
        ToolIcon {
            platformIconId: "toolbar-add";
            anchors.left: parent===undefined ? undefined : parent.left
            onClicked: { newDeviceSheet.open() }
            opacity: appWindow.pageStack.currentPage == targetsPage ? 1 : 0
            Behavior on opacity {
                NumberAnimation {}
            }
        }
    }

    Menu {
        id: myMenu
        visualParent: pageStack
        MenuLayout {
            MenuItem {
                text: qsTr("About Remote Control")
                onClicked: aboutDialog.open()
            }
        }
    }

    NewDeviceSheet {
        id: newDeviceSheet
    }

    AboutDialog {
        id: aboutDialog
    }

    Component.onCompleted: {
        inputContextRpc.setConnection(rpcClient)
    }
}
